#ifndef COAPTASK_H
#define COAPTASK_H

#include <string>
#include <doors/ptb.h>
#include <doors/udp.h>
#include <doors/ptask.h>
#include <doors/inetaddr.h>
#include <doors/timer.h>
#include "coapsap.h"
#include "coapsm.h"
#include "coappdu.h"

#include "coapconst.h"

class CoapTaskSM;

class CoapTask : public PTask 
{
public:
	CoapTask(std::string name, Scheduler* s, CoapTaskSM* sm);
	virtual ~CoapTask();
	
	UdpSAP :: User down;
	CoapSAP :: Provider up;
	CoapPeer peer; 

	//idle state
	bool idle_Default(Message* msg) { return false; }
	bool idle_Getreq(Message* msg);
	bool idle_Delreq(Message* msg);
	bool idle_Putreq(Message* msg);
	bool idle_GET(Message* msg);
	bool idle_DEL(Message* msg);
	bool idle_PUT(Message* msg);
	
	//wfAck state
	bool wfAck_Default(Message* msg) { return false; }
	bool wfAck_ACK(Message* msg);
	bool wfAck_Timeout(Message* msg);
	
	//wfDtresp state
	bool wfDtresp_Default(Message* msg) { return false; }
	bool wfDtresp_Dtrespout_pb(Message* msg);
	bool wfDtresp_Timeout(Message* msg);

	// Helper functions
private:	
	Uint16 getNewMessageID();

	void receiveGET(Message* msg);
	
	// Class variables
private:
	Uint16 messageid;
	Uint16 expected_messageid;
	Timer timer;
	int timer_retries;
};

#endif
